import { NgModule, LOCALE_ID, APP_INITIALIZER } from '@angular/core';
import {
  HttpClient,
  HTTP_INTERCEPTORS,
  HttpClientModule,
} from '@angular/common/http';
import { BrowserModule } from '@angular/platform-browser';
import { NoopAnimationsModule } from '@angular/platform-browser/animations';
import { APES_I18N, zh_CN } from '@shared/apes/i18n';
import { ApesReuseTabModule, ReuseTabService, ReuseTabStrategy } from '@shared/apes/reuse-tab';


import { CoreModule } from '@core/core.module';
import { SharedModule } from '@shared/shared.module';
import { AppComponent } from './app.component';
import { RoutesModule } from './routes/routes.module';
import { LayoutModule } from './layout/layout.module';
import { StartupService } from '@core/services/startup.service';
import { DefaultInterceptor } from '@core/net/default.interceptor';
import { InitService } from '@core/services/init.service';
import { UserAuthService } from '@core/services/user-auth.service';


// angular i18n
import { registerLocaleData } from '@angular/common';
import zh from '@angular/common/locales/zh';

registerLocaleData(zh);

const ngZorroConfig: ApesConfig = {
  message: { apesTop: 120 },
  notification: { apesTop: 240 },
};
// i18n
import { TranslateModule, TranslateLoader } from '@ngx-translate/core';
import { TranslateHttpLoader } from '@ngx-translate/http-loader';
import { APP_I18N_TOKEN } from '@core';
import { I18NService } from '@core/i18n/i18n.service';
import { RouteReuseStrategy } from '@angular/router';
import { ApesMeasureScrollbarService } from '@shared/apes/core/services/apes-measure-scrollbar.service';
import { DatePipe } from '@angular/common';
import { APES_CONFIG, ApesConfig } from '@shared/apes/core';
import { AnalysisService } from './routes/analysis/analysis.service';
import { ServiceWorkerModule } from '@angular/service-worker';
import { environment } from '../environments/environment';


// AoT requires an exported function for factories
export function HttpLoaderFactory(http: HttpClient) {
  return new TranslateHttpLoader(http, `assets/tmp/i18n/`, '.json');
}

export function StartupServiceFactory(
  startupService: StartupService,
): Function {
  return () => startupService.load();
}


@NgModule({
  declarations: [AppComponent],
  imports: [
    BrowserModule,
    NoopAnimationsModule,
    HttpClientModule,
    CoreModule.forRoot(),
    SharedModule.forRoot(),
    ApesReuseTabModule.forRoot(),
    LayoutModule,
    RoutesModule,
    // i18n
    TranslateModule.forRoot({
      loader: {
        provide: TranslateLoader,
        useFactory: HttpLoaderFactory,
        deps: [HttpClient],
      },
    }),
    ServiceWorkerModule.register('ngsw-worker.js', { enabled: environment.production }),
    // thirds

  ],
  providers: [
    { provide: LOCALE_ID, useValue: 'zh-Hans' },
    { provide: HTTP_INTERCEPTORS, useClass: DefaultInterceptor, multi: true },
    { provide: APP_I18N_TOKEN, useClass: I18NService, multi: false },
    { provide: APES_I18N, useValue: zh_CN },
    // { provide: APES_CONFIG,useValue: ngZorroConfig},
    {
      provide: RouteReuseStrategy,
      useClass: ReuseTabStrategy,
      deps: [ReuseTabService],
    },
    StartupService,
    InitService,
    UserAuthService,
    AnalysisService,
    ApesMeasureScrollbarService,
    {
      provide: APP_INITIALIZER,
      useFactory: StartupServiceFactory,
      deps: [StartupService],
      multi: true,
    },
    DatePipe,
  ],
  bootstrap: [AppComponent],
})
export class AppModule {
}
